package project.bob.entity;

import java.awt.*;
import java.awt.image.BufferedImage;

import project.bob.*;
import project.bob.inventory.HealItem;
import project.bob.inventory.Inventory;
import project.bob.level.*;

public class Player extends Entity {
	//TODO: Inventory, weapons. ||| PRIORETY 1
    private int dir = 1;
    private int yAim = 0;
    private int frame = 0;
    public int damage = 0;
    private int noHurtTime = 0;
    public int gunLevel = 0;
    public boolean readSign = false;
    private int shootTime = 0;
    public Inventory inv = new Inventory(4, 5, 140, 40, this);
    
    public int fullHealth = 100;
    public int health = 100;
    
    public Player(int x, int y) {
        this.x = x;
        this.y = y;
        w = 22;
        h = 44;
        bounce = 0;
    }

    public void tick() {
    }

    public void render(Graphics g, Camera camera) {
        //        g.setColor(Color.GREEN);
        int xp = (int) x - (16 - w) / 2;
        int yp = (int) y - 2;
        //      g.fillRect(xp, yp, w, h);

        int stepFrame = frame / 4 % 3;

        BufferedImage[][] sheet = dir == 1 ? Art.player1 : Art.player2;
        if (!onGround) {
            int yya = (int) Math.round(-ya);
            stepFrame = 5;
            if (yya < -1) stepFrame = 6;
            yp += yya;
        }
        g.drawImage(sheet[0 + stepFrame][0], xp, yp, null);


        yp += (stepFrame == 3 ? 1 : 0);

        if (gunLevel > 0) {
            if (!onGround) {
                int yya = (int) Math.round(-ya);
                if (yya < -1) yya = -1;
                if (yya > 1) yya = 1;
                yp += yya;
            }
            g.drawImage(sheet[0 + yAim][6], xp, yp, null); // g.drawImage(sheet[1 + yAim][(gunLevel - 1) * 2], xp, yp, null);
/*            if(blood){
            	g.drawImage(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9)
            }*/
        
        }
        //inv.render(g);
    }
    boolean blood = false;
    public void tick(Input input) {
        readSign = onGround && input.buttons[Input.UP] && !input.oldButtons[Input.UP];
        if (noHurtTime > 0) noHurtTime--;
        double speed = 0.4;
        double aimAngle = -0.2;
        yAim = 0;
        if(input.buttons[Input.G]){
            gunLevel = 1;
            level.getGun(1);
        }
/*        if (input.buttons[Input.UP]) {
            aimAngle -= 0.8;
            yAim--;
        }*/
        if (!inv.hasFocus)
        {
        if (input.buttons[Input.DOWN]) {
/*            aimAngle += 0.8;
            yAim++;*/
        	blood = true;
        }
        boolean walk = false; 
        if (input.buttons[Input.LEFT]) {
            walk = true;
            xa -= speed;
            dir = -1;
        }
        if (input.buttons[Input.RIGHT]) {
            walk = true;
            xa += speed;
            dir = 1;
        }
        if (walk) frame++;
        else frame = 0;
        if (input.buttons[Input.JUMP] && !input.oldButtons[Input.JUMP] && onGround) {
            //Sound.jump.play();
            ya -= 2 + Math.abs(xa) * 0.5;
        }
        
        if (input.buttons[Input.H]){
        	Sound.launch.play();
        	double pow = 3;
            double xx = x + w / 2.0 - 2.5 + dir * 7;
            double yy = y + h / 2.0/* - 2.5 + yAim * 2*/;
            for (int i = 0; i < 4; i++) {
                double xAim = Math.cos(aimAngle + 0.2) * dir * pow;
                double yAim = Math.sin(aimAngle + 0.2) * pow * dir;
/*                double xxa = xa + xAim * 0.2;
                double yya = ya + yAim * 0.8;*/
                level.add(new Bullet(null, xx, yy, xAim, yAim));
            }
        }
        
        
        if (input.buttons[Input.P] && !input.oldButtons[Input.P])
        {
        	HealItem heal = new HealItem(-1, -1, 1, true,inv);
        	heal.healPower = 10;
        	heal.name = "SMALL KIT";
        	heal.onGround = true;
        	heal.groundX = x-10;
        	heal.groundY = y-5; // test
        	ItemEntity  item = new ItemEntity(heal);
        	level.add(item);
        }
        }
        tryMove(xa, ya);

        xa *= 0.7;
        if (ya < 0 && input.buttons[Input.JUMP]) {
            ya *= 0.992;
            ya += Level.GRAVITY * 0.5;
        } else {
            ya *= Level.FRICTION;
            ya += Level.GRAVITY;
        }


        boolean shooting = false;
        if (gunLevel > 0 && input.buttons[Input.SHOOT] && !input.oldButtons[Input.SHOOT]) shooting = true;
        if (gunLevel > 1 && input.buttons[Input.SHOOT] && (!input.oldButtons[Input.SHOOT] || shootTime>0)) {
            shooting = shootTime++%3==0;
        } else {
            shootTime = 0;
        }
        if (shooting) {
            double pow = 3;
            Sound.launch.play();

            double xx = x + w / 2.0 - 2.5 + dir * 7;
            double yy = y + h / 2.0 - 2.5 + yAim * 2;
            for (int i = 0; i < 4; i++) {
                double xAim = Math.cos(aimAngle + 0.2) * dir * pow;
                double yAim = Math.sin(aimAngle + 0.2) * pow;
                double xxa = xa + xAim * 0.2;
                double yya = ya + yAim * 0.2;
                level.add(new Spark(xx, yy + (-2 + i) * 0.5, xxa, yya));
            }
            double xAim = Math.cos(aimAngle) * dir * pow;
            double yAim = Math.sin(aimAngle) * pow;
            double xxa = xa + xAim;
            double yya = ya + yAim;
            if (gunLevel==2) {
                xa-=xAim*0.1;
                ya-=yAim*0.1;
            }
            xx = x + w / 2.0 - 2.5;
            level.add(new Zombie(xx, yy, xxa, yya));
        }

        if (y < 5) level.transition(0, -1);
        if (y > 240 - w + 10 - 5) level.transition(0, 1);
        if (x < 0 + 5) level.transition(-1, 0);
        if (x > 320 - h + 10 - 5) level.transition(1, 0);
        
    }

    public void hitSpikes() {
        die();
    }

    public void die() {
        if (removed) return; 
      //  Sound.death.play();
        for (int i = 0; i < 16; i++) {
        }
        remove();
    }

    public boolean shot(Bullet bullet) {
      //  Sound.pew.play();
        xa += bullet.xa * 0.5;
        ya += bullet.ya * 0.5;
        for (int i = 0; i < 4; i++) {
            double xd = (random.nextDouble() - random.nextDouble()) * 4 - bullet.xa * 3;
            double yd = (random.nextDouble() - random.nextDouble()) * 4 - bullet.ya * 3;
        }
        if (noHurtTime != 0) return true;

        return true;
    }



    public void outOfBounds() {
    }

    public void explode(Explosion explosion) {
        die();
    }

    public void collideMonster(Entity e) {
        die();
    }

}
